package course.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import student.dao.DataBase;
import course.beans.Course;
public class StudentCourseDAO {
     private DataBase db;
     public StudentCourseDAO()
     {
    	 super();
    	 db=new DataBase();
     }
     public boolean addCourse(String stuNum,String courseId)
     {
     	boolean flag=false;
     	try
     	{
     		String sql="insert into studentCourse values('"+stuNum+"',"+"'"+courseId+"')";
     		db.executeUpdate(sql);
     		flag=true;
     	}
     	catch(SQLException ex)
     	{
     		ex.printStackTrace();
     	}
     	return flag;
     }
     public ArrayList<Course> getAllCourse(String id)
     {
    	 ArrayList<Course>courses=new ArrayList<Course>();
    	 CourseDAO couDao=new CourseDAO();
    	 try
     	{
     		String sql="select *from studentCourse where stuNum='"+id+"'";
     		ResultSet rst=db.executeQuery(sql);
     		while(rst.next())
     		{
     			String courseId=rst.getString("courseId");
     			Course course=couDao.getCourseInfo(courseId);
     			courses.add(course);
     		}
     	}
     	catch(SQLException ex)
     	{
     		ex.printStackTrace();
     	}
    	 return courses;
     }
     public boolean deleteCourse(String stuNum,String courseId)
     {
    	 boolean flag=false;
    	 try
    	 {
    		 String sql="delete from studentCourse where stuNum='"+
    		 stuNum+"' and courseId='"+courseId+"'";
    		 db.executeUpdate(sql);
    		 flag=true;
    	 }
    	 catch(SQLException ex)
    	 {
    		 ex.printStackTrace();
    	 }
    	 return flag;
     }
    
}
